package com.meichis.mcsappframework.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meichis.mcsappframework.R;
import com.meichis.mcsappframework.utils.UniversalInterface;

@SuppressLint("NewApi")
public class MobileVerifyDialog extends Dialog implements android.view.View.OnClickListener{

	private TextView tv_title,tv_mobile;
	private String mobile;
	private Button bt_getverifycode,bt_confirm,bt_cancel;
	private EditText et_verifycode;
	private static final int TIME = 60000;//60秒后重新发送
	private long time = TIME;
	private static final int MSG_TIME = 1;
	private UniversalInterface<Void, String> getVerifyInterface,confirmInterface;
	private Handler handler;
	private Context context;
	private LinearLayout ll_title;

	/**
	 *
	 * @param context
	 * @param mobileNum 发送的号码
	 * @param getVerifyInterface 点击获取校验码按钮执行操作
	 * @param confirmInterface 点击确认按钮执行操作，string为填写的验证码
     */
	public MobileVerifyDialog(Context context, String mobileNum, UniversalInterface<Void, String> getVerifyInterface,
							  UniversalInterface<Void, String> confirmInterface) {
		super(context,R.style.Appupdatedialog);
		this.getVerifyInterface = getVerifyInterface;
		this.confirmInterface = confirmInterface;
		this.context = context;
		mobile = mobileNum;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_mobileverify);
		try {
			this.dismiss();
		} catch (Exception e) {
		}
		handler = new Handler(){
			public void handleMessage(Message msg) {
				if (msg.what==MSG_TIME) {
					time = time-1000;
					if (time<=0) {
						bt_getverifycode.setText(context.getString(R.string.dialog_getverifycode));
						bt_getverifycode.setClickable(true);
						time=TIME;
					}else {
						bt_getverifycode.setText((time/1000)+context.getString(R.string.dialog_regetverifycode));
						bt_getverifycode.setClickable(false);
						handler.sendEmptyMessageDelayed(MSG_TIME, 1000);
					}

				}
			};
		};
		findViews();
		showContent();
	}

	private void findViews() {
		tv_mobile = (TextView) findViewById(R.id.tv_mobile);
		bt_getverifycode = (Button) findViewById(R.id.bt_getverifycode);
		et_verifycode = (EditText) findViewById(R.id.et_verifycode);
		bt_confirm =  (Button) findViewById(R.id.bt_confirm);
		bt_cancel =  (Button) findViewById(R.id.bt_cancel);
		ll_title = (LinearLayout) findViewById(R.id.ll_title);
		tv_title = (TextView) findViewById(R.id.tv_title);
		bt_confirm.setOnClickListener(this);
		bt_cancel.setOnClickListener(this);
		bt_getverifycode.setOnClickListener(this);
	}

	private void showContent() {
		tv_mobile.setText(mobile);

	}
	/**
	 * 设置标题和按钮的背景
	 * @param background
	 */
	public void setDefaultBackground(Drawable background){
		ll_title.setBackground(background);
		bt_cancel.setBackground(background);
		bt_getverifycode.setBackground(background);
	}

	/**
	 * 设置标题和按钮的背景
	 * @param resid
	 */
	public void setDefaultBackground(int resid){
		ll_title.setBackgroundResource(resid);
		bt_confirm.setBackgroundResource(resid);
		bt_cancel.setBackgroundResource(resid);
		bt_getverifycode.setBackgroundResource(resid);
	}

	/**
	 * 单独设置标题背景
	 * @param background
	 */
	public void setTitleBackground(Drawable background){
		ll_title.setBackground(background);
	}

	/**
	 * 单独设置标题背景
	 * @param resid
	 */
	public void setTitleBackground(int resid){
		ll_title.setBackgroundResource(resid);
	}

	/**
	 * 单独设置按钮背景
	 * @param background
	 */
	public void setButtonBackground(Drawable background){
		bt_confirm.setBackground(background);
		bt_cancel.setBackground(background);
		bt_getverifycode.setBackground(background);
	}

	/**
	 * 单独设置按钮背景
	 * @param resid
	 */
	public void setButtonBackground(int resid){
		bt_confirm.setBackgroundResource(resid);
		bt_cancel.setBackgroundResource(resid);
		bt_getverifycode.setBackgroundResource(resid);
	}
	/**
	 * 设置标题字体颜色
	 * @param color
	 */
	public void setTitleTextColor(int color){
		tv_title.setTextColor(color);
	}
	@Override
	public void onClick(View v) {
		if (v.getId()==R.id.bt_getverifycode) {
			getVerifyInterface.todo(tv_mobile.getText().toString());
			handler.sendEmptyMessage(MSG_TIME);
		}
		if (v.getId()==R.id.bt_confirm) {
			confirmInterface.todo(et_verifycode.getText().toString());
		}
		if (v.getId()==R.id.bt_cancel) {
			this.cancel();
		}
	}

}
